import threading
import time

lock = threading.Lock()


# 没问题
class Account:
    def __init__(self, balance):
        self.balance = balance

    def withdraw_money(self, account, amount):
        with lock:
            if account.balance >= amount:
                time.sleep(0.1)
                print('%s取钱成功' % threading.current_thread().name)
                account.balance -= amount
                print('余额：%s' % account.balance)
            else:
                print('%s取钱失败，余额不足' % threading.current_thread().name)


if __name__ == '__main__':
    accout = Account(1000)
    xiaoming = threading.Thread(target=accout.withdraw_money, args=(accout, 800), name='xiaoming')
    xiaohong = threading.Thread(target=accout.withdraw_money, args=(accout, 800), name='xiaohong')

    xiaoming.start()
    xiaohong.start()
